<template>
  <div class="login-container">
    <div class="login-card">
      <h1>登录</h1>
      <p v-if="error" class="error-message">{{ error }}</p>
      
      <form @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="email">邮箱</label>
          <input 
            type="email" 
            id="email" 
            v-model="email" 
            required 
            placeholder="请输入您的邮箱"
          >
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input 
            type="password" 
            id="password" 
            v-model="password" 
            required 
            placeholder="请输入您的密码"
          >
        </div>
        
        <button 
          type="submit" 
          class="btn-login" 
          :disabled="loading"
        >
          {{ loading ? '登录中...' : '登录' }}
        </button>
      </form>
      
      <div class="login-footer">
        <p>还没有账号？ <router-link to="/register">立即注册</router-link></p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { useUserStore } from '@/store/user';

const router = useRouter();
const route = useRoute();
const userStore = useUserStore();

// 表单数据
const email = ref('mcxing@139.com');
const password = ref('www7715626');
const error = ref('');
const loading = ref(false);

// 登录处理
const handleLogin = async () => {
  loading.value = true;
  error.value = '';
  
  try {
    const result = await userStore.login({
      email: email.value,
      password: password.value
    });
    
    if (result.success) {
      // 登录成功，重定向到原来要访问的页面或首页
      const redirectPath = route.query.redirect || '/';
      router.push(redirectPath);
    } else {
      error.value = result.error;
    }
  } catch (err) {
    error.value = '登录失败，请稍后再试';
    console.error('Login error:', err);
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 80vh;
  padding: 2rem;
}

.login-card {
  width: 100%;
  max-width: 400px;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  background-color: white;
}

h1 {
  text-align: center;
  margin-bottom: 1.5rem;
  color: #333;
}

.error-message {
  color: #ff3860;
  background-color: #feecf0;
  padding: 0.5rem;
  border-radius: 4px;
  margin-bottom: 1rem;
  font-size: 0.9rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
}

input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
}

input:focus {
  outline: none;
  border-color: #4CAF50;
  box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.2);
}

.btn-login {
  width: 100%;
  padding: 0.75rem;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.3s;
}

.btn-login:hover {
  background-color: #45a049;
}

.btn-login:disabled {
  background-color: #9e9e9e;
  cursor: not-allowed;
}

.login-footer {
  margin-top: 1.5rem;
  text-align: center;
}

.login-footer a {
  color: #4CAF50;
  text-decoration: none;
}

.login-footer a:hover {
  text-decoration: underline;
}
</style> 